package edu.zju.udms.dnavigator.core.server.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import edu.zju.udms.dnavigator.core.extract.ExtractEngineImpl;

@WebServlet("/api/admin")
public class ApiAdminServlet extends NavigatorServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8313090336722464067L;
	private static final Logger logger = Logger.getLogger(ApiAdminServlet.class);

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		logger.info("GET /api/admin"+req.getPathInfo());
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		logger.info("POST /api/admin");
		if (req.getParameter("cmd").equals("reextract")) {
			Thread extractEngineThread = new Thread(new ExtractEngineImpl(config, repository));
			extractEngineThread.start();
			try {
				extractEngineThread.join();
			} catch (Exception e) {
				logger.error("error when run /api/admin/reextract", e);
			}
		}
	}
}
